package com.logistic;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.Entity.Product.Product;
import com.Entity.Product.ProductImplement;
import com.Entity.Product.Product_tag;
import com.Entity.Product.Product_tagImplement;

public class Visitor extends Basic {
	private static final long serialVersionUID = 1L;
	/**
	 * @Title Visitor.java
	 * @Package com.logistic
	 * @author ufo
	 */

	private String[] visitorName;
	private String[] password;
	private String[] productId;
	private String[] product_tagNameId;
	private String[] max;
	private String[] first;

	public String viewOneTag() {
		Product_tagImplement tag = new Product_tag(getProduct_tagNameId()[0]);
		List<String> list = tag.getProductId(getMax()[0],getFirst()[0]);
		Iterator i = list.iterator();
		int j = 0;
		for (; i.hasNext(); j++) {
			result.put("productId" + j, (String) i.next());
		}
		result.put("flag", (j > 0 ? "true" : "false"));
		return SUCCESS;
	}

	public String viewOneProductTags() {
		System.out.println(1234);
		ProductImplement product = new Product(getProductId()[0]);
		System.out.println(12345);
		List<String> list = Product_tag.loadProduct(product);
		System.out.println(123456);
		getProduct_tags(list);
		return SUCCESS;
	}

	public String iteratorTags() {
		List list = Product_tag
				.iteratorTags(getMax()[0], getFirst()[0]);
		getProduct_tags(list);
		return SUCCESS;
	}

	private void getProduct_tags(List<String> product_tagNameId) {
		Iterator i = product_tagNameId.iterator();
		int j = 0;
		for (; i.hasNext(); j++) {
			Product_tagImplement product_tag = new Product_tag(i.next()
					.toString());
			result.put("product_tagName" + j, product_tag.getProduct_tagName());
			result.put("product_tagNameId" + j,
					product_tag.getProduct_tagNameId());
		}
		System.out.println(j);
		result.put("flag", (j > 0 ? "true" : "false"));
	}

	public String[] getMax() {
		return max;
	}

	public void setMax(String[] max) {
		this.max = max;
	}

	public String[] getFirst() {
		return first;
	}

	public void setFirst(String[] first) {
		this.first = first;
	}

	public String login() {
		visitor = new com.Entity.Visitor.Visitor(getVisitorName()[0],
				getPassword()[0]);

		if (visitor.isExist()) {
			addVisitorToSession();
			result.put("flag", "true");
		} else
			result.put("flag", "false");
		return SUCCESS;
	}

	public String viewProductOutline() {
		ProductImplement product = new Product(getProductId()[0]);
		Map<String, String> target = product.iterator();
		Set<String> set = target.keySet();
		for (Iterator i = set.iterator(); i.hasNext();) {
			String tmp = i.next().toString();
			result.put(tmp, target.get(tmp));
		}
		result.put("productId", getProductId()[0]);
		result.put("flag", set.size() > 0 ? "true" : "false");
		return SUCCESS;
	}

	public String viewOneProduct() {
		try {
			ProductImplement product = new Product(getProductId()[0]);
			Map<String, String> target = product.iterator();
			Set<String> set = target.keySet();
			for (Iterator i = set.iterator(); i.hasNext();) {
				String tmp = i.next().toString();
				result.put(tmp, target.get(tmp));
			}
			result.put("productId", getProductId()[0]);
			result.put("description", product.getDescription().toString());
			result.put("flag", set.size() > 0 ? "true" : "false");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	private void addVisitorToSession() {
		request.getSession().setAttribute("visitorName",
				visitor.getVisitorName());
		request.getSession().setAttribute("password", visitor.getPwd());
	}

	public String[] getVisitorName() {
		return visitorName;
	}

	public void setVisitorName(String[] visitorName) {
		this.visitorName = visitorName;
	}

	public String[] getPassword() {
		return password;
	}

	public void setPassword(String[] password) {
		this.password = password;
	}

	public String[] getProductId() {
		return productId;
	}

	public void setProductId(String[] productId) {
		this.productId = productId;
	}

	@Override
	public Map<String, String> getResult() {
		// TODO 自动生成的方法存根
		return this.result;
	}

	public String[] getProduct_tagNameId() {
		return product_tagNameId;
	}

	public void setProduct_tagNameId(String[] product_tagNameId) {
		this.product_tagNameId = product_tagNameId;
	}
}